package com.foo.component;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnStartService = findViewById(R.id.btn_start_service);
        Button btnStopService = findViewById(R.id.btn_stop_service);
        //服务的启动与销毁
        btnStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //从哪来到哪去
                Intent intent = new Intent(MainActivity.this, MyService.class);
                //启动服务
                startService(intent);
            }
        });

        btnStopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                stopService(intent);
            }
        });

        //意图过滤器
        IntentFilter intentFilter = new IntentFilter();
        //动态注册系统广播
        intentFilter.addAction("android.intent.action.AIRPLANE_MODE");
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁已注册的广播
        unregisterReceiver(myReceiver);
    }

    public void openCamera(View view) {
        //隐式意图
        Intent intent = new Intent();
        //打开相机意图
        intent.setAction("android.media.action.IMAGE_CAPTUER");
        //加上类别
        intent.addCategory("android.intent.category.DEFAULT");
        //启动意图
        startActivity(intent);

    }
}